<template>
  <a-card :bordered="false">
    <div class="card-title">{{$route.query.templateName}}-{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row>
        <a-col :span="6">
          <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button>
        </a-col>
      </a-row>
    </div>
    <s-table ref="table" rowKey="time_fragment_id" :loading="isLoading" :columns="columns" :data="loadData" :pageSize="15">
      <div class="actions" slot="action" slot-scope="text, item">
        <a v-action:edit @click="handleEdit(item)">编辑</a>
        <template>
          <a v-action:delete @click="handleDelete(item)">删除</a>
        </template>
      </div>
    </s-table>
    <AddForm ref="AddForm" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/time/template'
import { STable } from '@/components'
import AddForm from './time-fragment/AddForm.vue'
import EditForm from './time-fragment/EditForm.vue'

export default {
  name: 'TimeFragment',
  components: {
    STable,
    AddForm,
    EditForm,
  },
  data() {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '时间段ID',
          dataIndex: 'time_fragment_id',
          align: 'center',
        },
        {
          title: '开始时间',
          dataIndex: 'start',
          align: 'center',
        },
        {
          title: '结束时间',
          dataIndex: 'end',
          align: 'center',
        },
        {
          title: '排序',
          dataIndex: 'sort',
          align: 'center',
        },
        {
          title: '添加时间',
          dataIndex: 'create_time',
          align: 'center',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
          align: 'center',
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (param) => {
        // return Api.list({ ...param, ...this.queryParam })
        //   .then(response => {
        //     return response.data.list
        //   })
        const templateId = this.$route.query.templateId
        return Api.fragments({ 'time_template_id': templateId, ...param, ...this.queryParam }).then((response) => {
          console.log(response)
          return response.data
        })
      },
    }
  },
  methods: {
    /**
     * 新增记录
     */
    handleAdd() {
      const templateId = this.$route.query.templateId
      this.$refs.AddForm.add(templateId)
    },

    /**
     * 编辑记录
     */
    handleEdit(item) {
      this.$refs.EditForm.edit(item)
    },

    /**
     * 删除记录
     */
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.delFragment({ time_fragment_id: item['time_fragment_id'] })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally((result) => {
              modal.destroy()
            })
        },
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    /**
     * 检索查询
     */
    onSearch() {
      this.handleRefresh(true)
    },
   
  },
}
</script>

<style lang="less" scoped>
.template_name {
  cursor: pointer;
}
</style>